package com.webamc.manager;

import com.webamc.common.enums.Fields;
import com.webamc.common.enums.Params;
import com.webamc.dao.IOrderDao;
import com.webamc.model.Order;
import com.webamc.model.ProductOrder;
import com.webamc.util.CommonUtil;
import com.webamc.util.KeyUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * Date: Mar 8, 2009
 * Time: 3:06:33 AM
 */
public class ProductOrderHandler implements IOrderHandler {

    private Logger log = Logger.getLogger(ProductOrderHandler.class);
    private IOrderDao orderDao;

    public void setOrderDao(IOrderDao orderDao) {
        this.orderDao = orderDao;
    }

    public boolean create(Order u) {
        log.info("creating ProductOrder = " + u);
        u.setOrderId(KeyUtil.instance().getNextProductOrderId());
        return orderDao.create(u);
    }

    public boolean update(Order u) {
        log.info("updating ProductOrder = " + u);
        return orderDao.update(u);
    }

    public Order view(int id) {
        log.info("checking ProductOrderId= " + id);
        Order a = create();
        a.setOrderId(id);
        List<Order> l = search(a);
        return l.size() > 0 ? l.get(0) : null;
    }

    public List<Order> search(Order u) {
        log.info("searching ProductOrder= " + u);
        return orderDao.search(u);
    }

    public List<Order> browse() {
        return orderDao.browse();
    }

    public void update(Map<String, String[]> m, Order o) {
//        o.setParentOrderId(CommonUtil.getInt(m, Fields.PARENTORDERID, o.getParentOrderId()));
        o.setOrderId(CommonUtil.getInt(m, Fields.ORDERID, o.getOrderId()));
        o.setProductName(CommonUtil.getString(m, Fields.PRODUCTNAME, o.getProductName()));
        o.setCustomerId(CommonUtil.getInt(m, Fields.CUSTOMERID, o.getCustomerId()));
        o.setVersion(CommonUtil.getInt(m, Fields.VERSION, o.getVersion()));
        o.setProductSerial(CommonUtil.getString(m, Fields.PRODUCTSERIAL, o.getProductSerial()));
        o.setDescription(CommonUtil.getString(m, Fields.DESCRIPTION, o.getDescription()));
        o.setRemarks(CommonUtil.getString(m, Fields.REMARKS, o.getRemarks()));
        o.setCreateDate(CommonUtil.getTime(m, Fields.CREATEDATE, o.getCreateDate()));
        o.setCategory(CommonUtil.getString(m, Fields.ORDERCATEGORY, o.getCategory()));
        o.setStatus(CommonUtil.getString(m, Fields.ORDERSTATUS, o.getStatus()));

        ProductOrder p = (ProductOrder) o;
        p.setAssigneeId(CommonUtil.getInt(m, Fields.ASSIGNEEID, p.getAssigneeId()));
        p.setSupervisorId(CommonUtil.getInt(m, Fields.SUPERVISORID, p.getSupervisorId()));
        p.setWarranty(CommonUtil.getInt(m, Fields.WARRANTY, p.getWarranty()));
        p.setExtWarranty(CommonUtil.getInt(m, Fields.EXTWARRANTY, p.getExtWarranty()));
        p.setInstallationStatus(CommonUtil.getString(m, Fields.INSTALLATIONSTATUS, p.getInstallationStatus()));
        p.setInstallationDate(CommonUtil.getTime(m, Fields.INSTALLATIONDATE, p.getInstallationDate()));
        p.setInvoiceNumber(CommonUtil.getInt(m, Fields.INVOICENUMBER, p.getInvoiceNumber()));
        p.setInvoiceDate(CommonUtil.getTime(m, Fields.INVOICEDATE, p.getInvoiceDate()));
        p.setWarrantyEndDate(CommonUtil.getTime(m, Fields.WARRANTYENDDATE, p.getWarrantyEndDate()));

    }

    public ProductOrder create() {
        return new ProductOrder();
    }

    public String validate(Order u) {
        return Params.VALID;
    }

    public Order update(Map<String, String[]> m, int userId) {
        int id = CommonUtil.getInt(m, Fields.ORDERID, 0);

        Order p = view(id);
        update(m, p); //Param Validation
        validate(p);
        p.setVersion(1 + p.getVersion());
        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        update(p);

        return p;
    }

    public ProductOrder create(Map<String, String[]> m, int userId) {
        ProductOrder p = create();
        validate(p);
        update(m, p); //Param Validation

        p.setCreateDate(System.currentTimeMillis()); //one time
        p.setVersion(1);
        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        create(p);

        return p;
    }

    public List<Order> search(Map<String, String[]> m) {
        ProductOrder u = create();
        u.setOrderId(CommonUtil.getInt(m, Fields.ORDERID, u.getOrderId()));
        update(m, u);

        return search(u);
    }


}
